package percolator

import "hash/crc32"

const bitBucket = 1024
const bitSum = bitBucket << 6
const bitMod = bitSum - 1

func StringToHash(s string) int {
	v := int(crc32.ChecksumIEEE([]byte(s)))
	if -v >= 0 {
		v = -v
	}
	// bitBucket<<6表示位数组一共有个这么多个位
	v = (v ^ (v >> 16)) & bitMod
	return v
}

type BitSet struct {
	words []uint64
}

func (s *BitSet) Has(x int) bool {
	// x % 64 = x&(64-1)
	word, bit := x/64, uint(x&63)
	return word < len(s.words) && s.words[word]&(1<<bit) != 0
}

func (s *BitSet) Add(x int) {
	word, bit := x>>6, uint(x&63)
	s.words[word] |= 1 << bit
}

var bs = &BitSet{words: make([]uint64, bitBucket)}

func GetBloodFilter() *BitSet {
	return bs
}

func MakeBitSet() *BitSet {
	return &BitSet{words: make([]uint64, bitBucket)}
}
